/*
 * @Description: 知悉范围reducer
 * @Author: Rfan
 * @Date: 2022-04-07 15:49:26
 * @LastEditTime: 2022-04-15 18:32:26
 */

import { delItemByIdArray, insertItem } from '@/utils/list';
import {
  ADD_SCOPE,
  DELETE_SCOPE,
  SET_SCOPE_LIST,
  SET_SELECTED_ITEM,
  SET_SELECT_AT,
  SET_SELECT_FOND,
  SET_SELECT_LEVEL
} from '../constants';

export const ScopeOfKnowledgeReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中的全宗
     * @param {object} data 选中的全宗
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 设置选中的档案类型
     * @param {object} data 选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的层级
     * @param {object} data 选中的层级
     */
    case SET_SELECT_LEVEL:
      return {
        ...state,
        selectLevel: action.data
      };

    /**
     * @description: 设置知悉范围列表
     * @param {object} data 知悉范围列表
     */
    case SET_SCOPE_LIST:
      return {
        ...state,
        list: action.data
      };

    /**
     * @description: 新增知悉范围到列表
     * @param {object} data 新增的知悉范围规则
     */
    case ADD_SCOPE: {
      const list = insertItem(action.data, state.list);
      return {
        ...state,
        list
      };
    }

    /**
     * @description: 新增知悉范围到列表
     * @param {object} data 新增的知悉范围规则
     */
    case DELETE_SCOPE: {
      const list = delItemByIdArray(action.ids, state.list);
      return { ...state, list };
    }

    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    default:
      console.log('untouched', action);
      return state;
  }
};
